﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class CultureProfitForm : Form
    {
        public CultureProfitForm()
        {
            InitializeComponent();
        }

        int regID, avpID, fhID, pfshID = -1, cultID, year = DateTime.Now.Year;
        tb_cultureprofit cultureProfit;

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbFH.DataSource = null;
                cbPFSH.DataSource = null;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);

            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbFH.SelectedIndexChanged -= cbFH_SelectedIndexChanged;
                cbFH.Enabled = true;
                cbFH.DataSource = DBClass.Farms
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbFH.DisplayMember = "Name";
                cbFH.ValueMember = "ID";
                cbFH.SelectedIndex = -1;
                cbFH.Text = string.Empty;
                cbPFSH.DataSource = null;
                cbFH.SelectedIndexChanged += new EventHandler(cbFH_SelectedIndexChanged);
            }
        }

        private void cbFH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFH.SelectedIndex >= 0)
            {
                fhID = Convert.ToInt32(cbFH.SelectedValue);
                cbPFSH.Enabled = true;
                cbPFSH.SelectedIndexChanged -= cbPFSH_SelectedIndexChanged;
                cbPFSH.DataSource = DBClass.Schools
                    .Where(wh => wh.FarmID == fhID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbPFSH.DisplayMember = "Name";
                cbPFSH.ValueMember = "ID";
                cbPFSH.SelectedIndex = -1;
                cbPFSH.Text = string.Empty;
                cbPFSH.SelectedIndexChanged += new EventHandler(cbPFSH_SelectedIndexChanged);
            }
        }

        private void cbPFSH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbPFSH.SelectedIndex >= 0)
            {
                pfshID = Convert.ToInt32(cbPFSH.SelectedValue);
            }
            else
                pfshID = 0;
        }

        private void CultureProfitForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID; 
                cbRegion_SelectedIndexChanged(cbRegion, null);

                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            for (int i = 1991; i < 2100; i++)
            {
                cbYear.Items.Add(i);
            }
            cbYear.SelectedIndex = cbYear.FindStringExact(DateTime.Now.Year.ToString());

            cbCulture.SelectedIndexChanged -= cbCulture_SelectedIndexChanged;
            cbCulture.DataSource = UtilClass.GetCBCultureDataSourse();
            cbCulture.DisplayMember = "Name";
            cbCulture.ValueMember = "ID";
            cbCulture.SelectedIndex = -1;
            cbCulture.SelectedIndexChanged += new EventHandler(cbCulture_SelectedIndexChanged);
        }

        private void cbCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCulture.SelectedIndex >= 0)
            {
                cultID = Convert.ToInt32(cbCulture.SelectedValue);
                ShowCultureProfit(cultID, year, pfshID);
            }
            else
                cultID = 0;
        }

        private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbYear.SelectedIndex >= 0)
            {
                year = Convert.ToInt32(cbYear.Items[cbYear.SelectedIndex].ToString());
                ShowCultureProfit(cultID, year, pfshID);
            }
            else
                year = DateTime.Now.Year;
        }

        private void ShowCultureProfit(int cultID, int year, int pfshID)
        {
            cultureProfit = DBClass.CultProfits
                .Where(wh => wh.CultureID == cultID && wh.Date.Year == year && wh.SchoolID == pfshID)
                .SingleOrDefault();
            if (cultureProfit != null)
            {
                tbTransport.Text = cultureProfit.Transport.Value.ToString();
                tbGrossProduct.Text = cultureProfit.GrossProduct.Value.ToString();
                tbHarvest.Text = cultureProfit.Harvest.Value.ToString();
                tbWater.Text = cultureProfit.Water.Value.ToString();
                tbWaterDelivery.Text = cultureProfit.WaterDelivery.Value.ToString();
            }
            else
            {
                tbGrossProduct.Text = tbHarvest.Text = tbTransport.Text = tbWater.Text = tbWaterDelivery.Text = string.Empty;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            decimal transport, water, wDelivery, harvest, grProduct;

            if (tbGrossProduct.Text.GetDecimalOrDefault(out grProduct) &&
                tbHarvest.Text.GetDecimalOrDefault(out harvest) &&
                tbTransport.Text.GetDecimalOrDefault(out transport) &&
                tbWater.Text.GetDecimalOrDefault(out water) &&
                tbWaterDelivery.Text.GetDecimalOrDefault(out wDelivery) &&
                pfshID > 0 && cultID > 0)
            {

                if (cultureProfit == null)
                {
                    cultureProfit = new tb_cultureprofit
                    {
                        Date = new DateTime(year, 1, 1),
                        CultureID = cultID,
                        GrossProduct = grProduct,
                        Harvest = harvest,
                        SchoolID = pfshID,
                        Transport = transport,
                        Water = water,
                        WaterDelivery = wDelivery
                    };

                    DBClass.CultProfits.InsertOnSubmit(cultureProfit);
                }
                else
                {
                    cultureProfit.GrossProduct = grProduct;
                    cultureProfit.Harvest = harvest;
                    cultureProfit.Transport = transport;
                    cultureProfit.Water = water;
                    cultureProfit.WaterDelivery = wDelivery;
                }
                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
            }
            else
            {
                if (InformationClass.Lang)
                    MessageBox.Show("Не заполнены все необходимые поля. \nПроверьте правильность заполнения, выбранную с/х культуру и ПФШ");
                else
                    MessageBox.Show("Барча зарур сатрлар тўлдирилмаган. Танланган қ/х экини ва ДФМнинг тўғри тўлдирилганини текширинг");
                return;
            }
        }




    }
}
